// components/apply-form/index.js
import {ApplyForm} from "../models/apply"
import {Token} from "../../model/token"
import {Apply} from "../../model/apply"
import {User} from "../../model/user"

Component({
    /**
     * 组件的属性列表
     */
    properties: {},

    /**
     * 组件的初始数据
     */
    data: {
        items: [
            {value: '男', name: '男', checked: true},
            {value: '女', name: '女', checked: false}
        ],
        region: ['上海市'],
        province: null,
        city: null,
        area: null,
        name: null,
        age: null,
        school: null,
        mobile: null,
        missingValue: true,
        code: ''
    },
    apply: null,
    lifetimes: {
        async attached() {
            this.apply = new ApplyForm(this.data.items)
            const r = await wx.login()
            this.setData({
                code: r.code
            })
        },
    },
    observers: {
        'items, area, name, age, school, mobile': function (items, area, name, age, school, mobile) {
            const values = [items, area, name, age, school, mobile]
            const missingValue = this.apply.findEmptyValue(values)
            this.setData({
                missingValue
            })
        }
    },
    /**
     * 组件的方法列表
     */
    methods: {
        radioChange(e) {
            const items = this.apply.handleGenderStatus(e.detail.value)
            this.setData({
                items: items
            })
        },
        async submit(e) {
            let that = this
            const {name, age, gender, region, school} = e.detail.value
            if (!this.data.missingValue) {
                wx.showLoading({
                    mask: true
                })
                wx.getUserProfile({
                    desc: '获取您的昵称和头像',
                    success: async (res) => {
                        wx.setStorageSync('userInfo', res.userInfo)
                        const user = new User()
                        user.updateUserInfo({
                            source_name: res.userInfo.nickName,
                            source_avatar: res.userInfo.avatarUrl
                        }).then(async res => {
                            const apply = await Apply.applyJoin({
                                name,
                                age,
                                gender: gender === '男' ? 1 : 0,
                                area: region.join(','),
                                mobile: that.data.mobile,
                                school_name: school
                            })
                            if (apply) {
                                wx.hideLoading()
                                wx.showToast({
                                    icon: "success",
                                    title: '报名成功',
                                    duration: 1000
                                })
                                wx.navigateBack()
                            } else {
                                wx.showToast({
                                    icon: "none",
                                    title: '请重试',
                                    duration: 1000
                                })
                            }
                        })
                    },
                    fail: (err) => {
                        wx.hideLoading()
                    }
                })
            }
        },
        async getPhoneNumber(e) {
            wx.showLoading({
                mask: true
            })
            const {encryptedData, iv} = e.detail
            const token = new Token()
            const mobile = await token.decodeFromServer(encryptedData, iv, this.data.code)
            if (mobile) {
                wx.hideLoading()
                this.setData({
                    mobile: mobile.phoneNumber
                })
            } else {
                wx.hideLoading()
                wx.showToast({
                    icon: 'none',
                    title: '请重试',
                    duration: 3000
                })
            }
        },
        bindRegionChange(e) {
            const {value} = e.detail
            if (value[0] === '上海市') {
                this.setData({
                    province: value[0],
                    city: value[1],
                    area: value[2]
                })
            } else {
                wx.showToast({
                    icon: "none",
                    title: '该赛事只限上海区域报名',
                    duration: 3000
                })
            }
        },
        nameInputChange(e) {
            this.setData({
                name: e.detail.value
            })
        },
        ageInputChange(e) {
            this.setData({
                age: e.detail.value
            })
        },
        schoolInputChange(e) {
            this.setData({
                school: e.detail.value
            })
        }
    }
})
